// 班组管理

import localforage from "localforage"
localforage.config({
  // driver: [localforage.INDEXEDDB],
  name: 'INDEXDB',
  version: "1.0"
})
let type = "team"
export default {
  async create(data) {
    data = JSON.parse(JSON.stringify(data))
    let workers = await localforage.getItem(type) || new Map()
    // 生成id
    let keys = [...workers.keys()].sort((a, b) => a - b)
    let leng = keys.length ? keys[keys.length - 1] : 0
    let uid = data.uid ? data.uid : leng + 1
    data.uid = uid
    workers.set(uid, data)
    return localforage.setItem(type, workers)
  },

  async query({name}) {
    let workers = await localforage.getItem(type) || new Map()
    let data = [...workers.values()].filter(item => item.name.includes(name))
    return data
  },

  async delete(keys) {
    let workers = await localforage.getItem(type) || new Map()
    let a = keys.forEach(item => workers.delete(item))
    return localforage.setItem(type, workers)
  },
}